package com.moleq.remote.rpt;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class DatabaseResponseManager {
	
	private SQLiteDatabase db;
	
	
	public DatabaseResponseManager(){
		String dbPath = "/data/data/com.moleq.posdb/databases/mpos.db";
		opendb(dbPath);
	}
	
	public void opendb(String dbPath) {
		db = SQLiteDatabase.openOrCreateDatabase(dbPath, null);

	}

	public void closedb() {
		if (db != null) {
			db.close();
		}
	}

	public boolean executeNonQuery(String sql) {
		boolean result = false;
		try {
			db.execSQL(sql);
			result = true;
		} catch (Exception e) {
			// TODO: handle exception
			result = false;
		}
		return result;
	}
	
	public String rawQuery(String sql) {
		String result = "";
		Cursor cursor = db.rawQuery(sql, null);
		
		int colCnt = 0;
		if (cursor != null) {
			colCnt = cursor.getColumnCount();
			for (int i = 0; i < colCnt; i++) {
				result += cursor.getColumnName(i) +"  |  ";
			}
			
			result += "\r\n";
			
			while (cursor.moveToNext()) {
				for (int i = 0; i < colCnt; i++) {
					result += cursor.getString(i) +"  |  ";
				}
				result += "\r\n";
			}
		}
		
		return result;
	}

}
